#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <iomanip.h>
#include <stdlib.h>
                //1 2 3 4 5
int row1[5] = {0,1,0,1,0};
int row2[5] = {0,0,0,0,0};
int row3[5] = {0,0,0,0,1};
int row4[5] = {0,0,1,0,0};
int row5[5] = {0,1,0,0,0};

int* map[5] = {row1, row2, row3, row4, row5};

struct mrec
{
  int us;
  mrec *next;
};

mrec *mmat[5];
bool first = true;

void make_mrec(int** map)
{
  mrec *ptr;
  for (int i=0; i<5; i++)
  {
    first = true;
    for (int j=0; j<5; j++)
    {
      if (map[i][j] != 0)
      {
        if (first == 1)
        {
          first = 0;
          ptr = new mrec;
          mmat[i] = ptr;
          ptr->next = NULL;
          ptr->us = j;
        }
        else
        {
          ptr->next = new mrec;
          ptr = ptr->next;
          ptr->us = j;
          ptr->next = NULL;
        }
      }
    }
  }
}

void ShiTuWanZheng(int **map)
{
  for (int i=0; i<5; i++)
  {
    for (int j=0; j<5; j++)
    {
      for (int k=0; k<5; k++)
      {
        if (map[i][j] == 0)
        {
          map[i][j] = map[i][k] * map[k][j];
        }
      }
    }
  }
}

void PanDuanShiFouKeDaDao(int **map)
{
  for (int i=0; i<5; i++)
  {
    cout<< (i+1)<< "  ";
    for(int j=0; j<5; j++)
    {
      if ((map[i][j] == 0) && (i != j))
      {
        cout<< " "<< (j+1);
      }
    }
    cout<< endl;
  }
}

void display(int **data)
{
  for (int i=0; i<5; i++)
  {
    for (int j=0; j<5; j++)
    {
      cout <<setw(3) <<data[i][j]; // *(*(data+i)+j)
    }
    cout <<endl;
  }
  cout<<endl;
  //getch();
}


void main()
{
  clrscr();
  make_mrec(map);
  display(map);
  ShiTuWanZheng(map);
  display(map);
  PanDuanShiFouKeDaDao(map);
  getch();
}
